import { Controller } from 'egg';

export default class VideoController extends Controller {
  async list() {
    const id = this.ctx.request.queries.id;
    this.ctx.body = await this.app.mysql.select('video', {
      where: { course_id: +id },
      orders: [['id', 'desc']],

    });
  }

  async create() {
    const data: any = {};
    console.log(123)
    const body = this.ctx.request.body;
    data.course_id = body.course_id;
    if (body.id) data.id = body.id;
    if (body.title) data.title = body.title;
    if (body.brief_descript) data.brief_descript = body.brief_descript;
    if (body.serials) data.serials = body.serials;
    if (body.pictures) data.pictures = body.pictures;
    if (body.teach_content) data.teach_content = body.teach_content;
    if (body.video_url) data.video_url = body.video_url;
    if (body.video_content) data.video_content = body.video_content;
    try {
      console.log(data)
      if (!body.id)
        await this.app.mysql.insert('video', data);
      else
        await this.app.mysql.update('video', data);
      this.ctx.body = { status: 0 }
    } catch (e) {
      this.ctx.body = { status: -1, message: e }
    }
  }

  async delete() {
    const { id } = this.ctx.params;
    try {
      await this.app.mysql.delete('video', { id });
      this.ctx.body = { status: 0 };
    } catch (e) {
      this.ctx.body = { status: -1, message: e };
    }
  }

}
